import { createRouter, createWebHistory } from 'vue-router'
import HomeView from "@/views/HomeView";
import NotFound from "@/components/NotFound";
import ModelView from "@/views/Model/Statistic";
import userLoginView from "@/views/user/UserLoginView.vue";
import userRegisterView from "@/views/user/UserRegisterView.vue";
import store from "@/store";

const routes = [
  {
    path: '/',
    name: 'home',
    component:HomeView,
    meta:{
      requestAuth:true,
    }
  },
  {
    path: '/training',
    name: 'model_index',
    component:ModelView,
    meta:{
      requestAuth:true,
    }
  },

  {
    path: '/login',
    name: 'Login',
    component:userLoginView,
    meta:{
      requestAuth:false,
    }
  },
  {
    path: '/register',
    name: 'Register',
    component:userRegisterView,
    meta:{
      requestAuth:false,
    }
  },
  {
    path: '/recommend',
    name: 'BaseRecommend',
    component: () => import('@/views/Recoomend/RecommendIndexView.vue'),
    meta:{
      requestAuth:true,
    }
  },

  {
    path:'/404',
    name:'404',
    component:NotFound,
    meta:{
      requestAuth:false,
    }
  },
  {
    path:'/:catchAll(.*)',
    redirect: '/404'
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes
});

router.beforeEach(
    (to, from, next) => {

      if(to.meta.requestAuth && !store.state.user.is_login){
        next({name:"Login"});
      }else{
        next();
      }
    });

export default router
